package oop.ex1.actions;

import java.io.File;
import java.io.IOException;

//###############
//FILE : Action.java
//WRITER : Amir Wilf, wilfoa, 300007085
//WRITER : Daniel Acherkan, danithelion, 310116348
//EXERCISE : oop ex1 2011
//DESCRIPTION:
//An action that changes a certain attribute of a file (readable, writable, executable).
//###############
public class AttribAction implements Action{

	//Constants:
	private final char READABLE = 'r';
	private final char WRITABLE = 'w';
	private final char EXECUTABLE = 'x';
	
	//Data Members:
	protected char _attribute;
	protected boolean _degree;
	
	/**
	 * A Constructor for the class.
	 */
	public AttribAction(char attribute, boolean degree){
		_attribute = attribute;
		_degree = degree;
	}
	
	/**
	 * Turns a file's readable\writable\executable attribute to Y\N, 
	 * according to the filter's specification
	 * @param file the file whose attribute is changed. 
	 */
	@Override
	public void doAction(File file) throws IOException {
		switch(_attribute){
			case READABLE:{
				if(!file.setReadable(_degree)){
					throw new IOException();
				}
				break;
			}
			case WRITABLE:{
				if(!file.setWritable(_degree)){
					throw new IOException();
				}
				break;
			}
			case EXECUTABLE:{
				if(!file.setExecutable(_degree)){
					throw new IOException();
				}
				break;
			}
		}
	}

}
